package com.ice.body.rpc.api;

import com.ice.base.util.RedisUtil;
import com.ice.body.business.model.ReagentStock;
import com.ice.body.business.service.ReagentStockService;
import com.ice.body.rpc.RfidModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * rpc接收结算台的rfid扫描的数据
 * 进行出入库结算
 */
@Slf4j
@RestController
public class ReceiveRfidApi {

    @Resource
    private ReagentStockService reagentStockService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 一体机扫描服务 传送过来一个
     * 服务放在服务器上，rfid扫描放在各个房间，所以需要rpd调用传输
     * warehouseId 为小服务设置好的，epc为扫码的结果
     * 涉及多实验室区别扫描时 要标记数据归属
     * @param model
     */
    @RequestMapping("/api_p/receiveRfidData")
    public void receiveRfidData(@RequestBody RfidModel model) {
        String warehouseId = model.getWarehouseId();
        String epcCode = model.getData();
        log.info("接收rfid扫描：实验室id：" + model.getWarehouseId()  + ",epcCode=" + model.getData());

        try{

            ReagentStock stockModel = reagentStockService.getByEpcCode(epcCode);
            if(null == stockModel){
                log.error("不存在的标签：" + epcCode);
                return;
            }else if (stockModel.getState().equals("0")) {
                List<ReagentStock> rkList = redisUtil.get(warehouseId + "rkList") == null ? new ArrayList<>() : (ArrayList) redisUtil.get(warehouseId + "rkList");   //入库
                for (ReagentStock rkModel:rkList ){
                    if (rkModel.getEpcCode().equals(epcCode)) {
                        log.error("入库重复扫描标签：" + epcCode);
                        return;
                    }
                }

               // stockModel.setState("1");//在库
                rkList.add(stockModel);
                redisUtil.set(warehouseId + "rkList", rkList);
            //0待入库,1在库;2借出;8销毁,9过期
            } else if (stockModel.getState().equals("2")) {
                List<ReagentStock> ghList = redisUtil.get(warehouseId + "ghList") == null ? new ArrayList<>() : (ArrayList) redisUtil.get(warehouseId + "ghList");   //归还
                for (ReagentStock ghModel:ghList ){
                    if (ghModel.getEpcCode().equals(epcCode)) {
                        log.error("归还重复扫描标签：" + epcCode);
                        return;
                    }
                }
                stockModel.setState("2");//归还
                ghList.add(stockModel);
                redisUtil.set(warehouseId + "ghList", ghList);
            } else if (stockModel.getState().equals("1")) {
                List<ReagentStock> ckList = redisUtil.get(warehouseId + "ckList") == null ? new ArrayList<>() : (ArrayList) redisUtil.get(warehouseId + "ckList");   //出库
                for (ReagentStock ckModel:ckList ){
                    if (ckModel.getEpcCode().equals(epcCode)) {
                        log.error("出库重复扫描标签：" + epcCode);
                        return;
                    }
                }

                if (ckList.contains(epcCode)) {
                    log.error("出库重复扫描标签：" + epcCode);
                    return;
                }

                ckList.add(stockModel);
                redisUtil.set(warehouseId + "ckList", ckList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}